package com.etrans.web.gps.business.log.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.log.domain.OperateRecord;
import com.etrans.web.gps.business.log.domain.OperateRecordvo;
import com.etrans.web.gps.business.log.service.OperationRecordService;
import com.etrans.web.gps.business.system.domain.SysUser;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.grid.FlexiGridUtil;

@Controller
@RequestMapping("/log/OperationRecord/")
public class OperationRecordController extends BaseController{
	
	public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Autowired
	private OperationRecordService operationRecordService;
	
	@RequestMapping("toOperateRecord.html")
	public String  toOperateRecord(HttpServletResponse respone,String paramsGrid){
		return "views/business/log/operateRecord";
	}
	
	
	@RequestMapping("getOperationRecord.html")
	public @ResponseBody PageVo  getOperationRecord(HttpServletResponse respone,String paramsGrid){
		OperateRecordvo  vo = new OperateRecordvo();
		Map<String,Object> map = FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
		int userid = this.getSysUser().getId();
		vo.setUserid(userid);
		String moduleName = (String) map.get("moduleName");
		String startTime = (String) map.get("startTime");
		String endTime = (String) map.get("endTime");
		if(moduleName!=null&&!"".equals(moduleName)){
			vo.setModuleName(moduleName);
		}
		try {
			if(startTime!=null&&!startTime.isEmpty()){
				vo.setStartTime(SDF.parse(startTime));
			}
			if(endTime!=null&&!endTime.isEmpty()){
				vo.setEndTime(SDF.parse(endTime));
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PageVo list = operationRecordService.getRecord(vo,map);
		return list;

	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "exportRecord.html")
	public void exportRecord( HttpServletRequest request, HttpServletResponse response, String paramsGrid, String colModel, String title, String StartPage, String EndPage, String pagesize) {
		Map<String, Object> paramMap=getReqParameters();
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		SysUser user = getSysUser();
		List<String> whereParamsList = JSON.parseObject(paramMap.get("queryParams").toString(), List.class);
		HashMap<String, Object> param = new HashMap<String, Object>();
		for (Object object : whereParamsList) {
			JSONObject jsonObject = (JSONObject) object;
			param.put(jsonObject.get("name").toString(),jsonObject.get("value"));
		}
		OperateRecordvo  vo = new OperateRecordvo();	
		param.put("page",Integer.parseInt(StartPage));
		param.put("userId", user.getId());
		param.put("pageSize", (Integer.parseInt(EndPage) - Integer.parseInt(StartPage)+1)*Integer.parseInt(pagesize));
		paramMap.putAll(param);

		
		vo.setUserid(user.getId());
		String moduleName = (String) param.get("moduleName");
		String startTime = (String) param.get("startTime");
		String endTime = (String) param.get("endTime");
		
		if(moduleName!=null&&!"".equals(moduleName)){
			vo.setModuleName(moduleName);
		}
		try {
			if(startTime!=null&&!startTime.isEmpty()){
				vo.setStartTime(SDF.parse(startTime));
			}
			if(endTime!=null&&!endTime.isEmpty()){
				vo.setEndTime(SDF.parse(endTime));
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PageVo pageVo = operationRecordService.getRecord(vo,param);
		List<OperateRecord> list = (List<OperateRecord>)pageVo.getRows();
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		int start=Integer.valueOf(Integer.valueOf(StartPage)-1)*Integer.valueOf(pagesize);
		int end=Integer.valueOf(EndPage)*Integer.valueOf(pagesize);
		if(end>(list.size()-1)){
			end=list.size();
		}
		for(int i=start;i<end;i++){
			HashMap<String, Object> map = new HashMap<String, Object>();
			
			OperateRecord obj=(OperateRecord)list.get(i);
			map.put("userName", obj.getUserName()==null ? "" : obj.getUserName());
			map.put("moduleName", obj.getModuleName()==null ? "" : obj.getModuleName());
			map.put("operationTime", obj.getOperationTime()==null ? "" : SDF.format(obj.getOperationTime()));
			map.put("operationDescribe", obj.getOperationDescribe()==null ? "" : obj.getOperationDescribe());
			rows.add(map);
		}
		int index=-1;
		@SuppressWarnings("rawtypes")
		List rowsList = new ArrayList<>();
		rowsList.add(rows);
		exportExl(request, response,title, removeHeadName(colModelList,index,-1),rowsList,null);
	}
	
	
}
